package cn.tedu.imop.marketing.service.impl;

import cn.tedu.imop.common.context.PageContext;
import cn.tedu.imop.common.pojo.PageData;
import cn.tedu.imop.marketing.dao.repository.ActivityRepository;
import cn.tedu.imop.marketing.dao.repository.CouponRepository;
import cn.tedu.imop.marketing.dao.repository.MemberCouponRepository;
import cn.tedu.imop.marketing.entity.Activity;
import cn.tedu.imop.marketing.entity.MemberCoupon;
import cn.tedu.imop.marketing.service.MemberCouponService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Service
public class MemberCouponServiceImpl implements MemberCouponService {
    private ActivityRepository activityRepository;
    private MemberCouponRepository memberCouponRepository;
    private CouponRepository couponRepository;
    public MemberCouponServiceImpl(
            ActivityRepository activityRepository,
            MemberCouponRepository memberCouponRepository) {
        this.memberCouponRepository = memberCouponRepository;
        this.activityRepository = activityRepository;
    }
    @Transactional
    @Override
    public Boolean createMemberCoupon(MemberCoupon memberCoupon) {
        //判断用户是否已认证(登录，没有登录需要先登录)
        //判断用户是否是活动目标人群。
        Integer memberId=1;//登录用户id(先给一个固定值)
        isTargetCustomer(memberId, memberCoupon.getActivityId());
        //判断活动的有效性
        checkActivity(memberCoupon.getActivityId());
        //判断用户是否已领取优惠卷，假如已领不可以再领取
        isChaimedCoupon(memberId,memberCoupon.getCouponId(),memberCoupon.getCouponCode());
        //领取优惠卷(这里需要判断库存够吗,还需要保证同一张卷只能被一个用户领取)
        memberCouponRepository.insertMemberCoupon(memberCoupon);
        //扣减优惠卷库存(可以先留钩子，这里要调用库存服务):异步，事务
        //......
        return true;
    }

    private void isChaimedCoupon(Integer memberId, Integer couponId, String couponCode) {
          int nums=memberCouponRepository.countMemberCoupon(memberId, couponId, couponCode);
          if(nums>0)throw new RuntimeException("用户只能领取一次");
    }

    private void checkActivity(Integer activityId) {
        Activity activity = activityRepository.selectActivityById(activityId);
        Date currentDate=new Date();
        boolean flag=currentDate.before(activity.getStartDate());
        if(flag) throw new RuntimeException("活动未开始");
        flag=currentDate.after(activity.getEndDate());
        if(flag) throw new RuntimeException("活动已结束");
    }

    private void isTargetCustomer(Integer memberId, Integer activityId) {
        String targetCustomerIds=
                activityRepository.selectTargetCustomers(activityId);
        //用户第一次登录时，会选择用户喜好(给自己贴个标签)
        //调用远程服务，基于目标人群id，获取人群用户标签，然后基于人群标签，获取用户id
        //......
    }

    @Override
    public PageData<MemberCoupon> getMemberCouponList(Integer memberId, Integer status) {
        //判断用户是否已登录
        PageData<MemberCoupon> memberCouponPageData =
                memberCouponRepository.selectList(memberId, status);
        return memberCouponPageData;
    }

    @Override
    public MemberCoupon getMemberCouponById(Integer id) {
        //判断用户是否已登录
        return memberCouponRepository.selectMemberCouponById(id);
    }

    @Override
    public Boolean updateMemberCoupon(MemberCoupon memberCoupon) {
        return memberCouponRepository.updateMemberCoupon(memberCoupon)>0;
    }
}
